package com.yj.ccs.abs.common.exception;

import com.yj.ccs.abs.common.model.BaseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * Copyright 2018 (C) Yunjian-VC
 * Created on : 2018/1/9
 * Author     : Unbelievable9
 **/
@ControllerAdvice
public class CustomExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(CustomExceptionHandler.class);

    private static final String USER_UNAUTHORIZED_FLAG = "User Unauthorized";
    private static final String USER_FORBIDDEN_FLAG = "User Role Type Wrong";

    @ExceptionHandler(Exception.class)
    public ResponseEntity<BaseResult> handleGeneralException(Exception exception) {
        return new ResponseEntity<>(BaseResult.error(exception.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    public ResponseEntity<BaseResult> handleHttpMessageNotReadableException(HttpMessageNotReadableException exception) {
        logger.error(exception.getMessage());

        return new ResponseEntity<>(BaseResult.error(exception.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
    }

    @ExceptionHandler(BadSqlGrammarException.class)
    public ResponseEntity<BaseResult> handleBadSqlGrammarException(BadSqlGrammarException exception) {
        logger.error(exception.getMessage());

        return new ResponseEntity<>(BaseResult.error("SQL语法错误").setData(exception.getSQLException().getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
    }

    @ExceptionHandler(DataIntegrityViolationException.class)
    public ResponseEntity<BaseResult> handleDataIntegrityViolationException(DataIntegrityViolationException exception) {
        logger.error(exception.getMessage());

        return new ResponseEntity<>(BaseResult.error("数据更新错误"), HttpStatus.INTERNAL_SERVER_ERROR);
    }

    @ExceptionHandler(BindException.class)
    public ResponseEntity<BaseResult> handleMethodArgumentNotValidException(BindException exception) {
        logger.error(exception.getMessage());

        List<String> errorList = new ArrayList<>();

        for (ObjectError objectError : exception.getBindingResult().getAllErrors()) {
            errorList.add(objectError.getDefaultMessage());
        }

        return new ResponseEntity<>(BaseResult.error(errorList.get(0)), HttpStatus.BAD_REQUEST);
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseEntity<BaseResult> handleMethodArgumentNotValidException(MethodArgumentNotValidException exception) {
        logger.error(exception.getMessage());

        List<String> errorList = new ArrayList<>();

        for (ObjectError objectError : exception.getBindingResult().getAllErrors()) {
            errorList.add(objectError.getDefaultMessage());
        }

        return new ResponseEntity<>(BaseResult.error(errorList.get(0)), HttpStatus.BAD_REQUEST);
    }



    @ExceptionHandler(UserUnauthorizedException.class)
    public ResponseEntity<BaseResult> handle401Exception() {
        return new ResponseEntity<>(BaseResult.error(USER_UNAUTHORIZED_FLAG), HttpStatus.UNAUTHORIZED);
    }

    @ExceptionHandler(UserForbiddenException.class)
    public ResponseEntity<BaseResult> handle403Exception() {
        return new ResponseEntity<>(BaseResult.error(USER_FORBIDDEN_FLAG), HttpStatus.FORBIDDEN);
    }
}